<?php
class Daotao_Model_Item extends Zend_Db_Table{
	
	protected $_name = 'nam_hopdong';
	protected $_primary ='id';
	protected $_ids;
	
	//Cảnh 
	public function getItem($arrParam = null, $options = null) {
	
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db	->	select()
							->	from('nam_hopdong AS hd', array('*','DATE_FORMAT(hd.ngayky, "%d/%m/%Y") AS ngayky'))
							-> 	joinLeft('nam_daotao_lophoc AS lh', 'lh.id = hd.id_lophoc', array('lh.name AS lophoc_name'))
							-> 	joinLeft('nam_daotao_khoahoc AS kh', 'kh.id = hd.id_khoahoc', array('kh.name AS khoahoc_name', 'kh.price', 'kh.id AS id_khoahoc'))
							->	where('hd.id = ?', $arrParam['id']);
	
			$result = $db	->	fetchRow($select);
			return $result;
		}
	}
	
	// Cảnh
	public function countItem($arrParam = null, $options = null) {
		$ssFilter  = $arrParam['ssFilter'];
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
	
	if ($options['task'] == 'admin-list') {
			$select = $db	-> 	select()
							-> 	from('nam_hopdong AS hd', array('COUNT(hd.id) AS totalItem'))
							->	where('hd.status != ?', -1);
			
			if (!empty($arrParam['id_kh'])) {
				$select 	->	where('hd.id_kh = ?', $arrParam['id_kh']);
			}
			
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
			
			if ($ssFilter['id_khoahoc'] > 0 ) {
				$select 	->	where('hd.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
			
			if ($ssFilter['id_lophoc'] > 0) {
				$select 	->	where('hd.id_lophoc = ?', $ssFilter['id_lophoc']);
			}
			
			if (!empty($ssFilter['keywords'])) {
				$idHd = explode('-', $ssFilter['keywords']);
				if(!empty($idHd[1])){
					$select->where("hd.id = ?", $idHd[1]);
				}
				$select->where("(kh.name LIKE '%" . $idHd[0] . "%') OR (kh.code LIKE '%" . $idHd[0] . "%') OR (kh.email LIKE '%" . $idHd[0] . "%') OR (kh.phone LIKE '%" . $idHd[0] . "%') OR (kh.telephone LIKE '%" . $idHd[0] . "%') OR (kh.fax LIKE '%" . $idHd[0] . "%') OR (kh.address LIKE '%" . $idHd[0] . "%')");
			}
				
			if (!empty($ssFilter['blocks'])) {
				if($ssFilter['blocks'] == 'nohocphi') {
					$select 	->	where('hd.hd_sotienno > ?', 0);
				}
				if($ssFilter['blocks'] == 'chuavaolop') {
					$select 	->	where('hd.id_lophoc = ?', 0);
				}
			}
			
			$result = $db	-> fetchOne($select);
		}
		
		if ($options['task'] == 'admin-recyclebin') {
			$select = $db	-> 	select()
			-> 	from('nam_hopdong AS hd', array('COUNT(hd.id) AS totalItem'))
			->	where('hd.status =  -1');
				
			if (!empty($arrParam['id_kh'])) {
				$select 	->	where('hd.id_kh = ?', $arrParam['id_kh']);
			}
				
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
				
			if ($ssFilter['id_khoahoc'] > 0 ) {
				$select 	->	where('hd.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
				
			if ($ssFilter['id_lophoc'] > 0) {
				$select 	->	where('hd.id_lophoc = ?', $ssFilter['id_lophoc']);
			}
				
			if (!empty($ssFilter['keywords'])) {
				$idHd = explode('-', $ssFilter['keywords']);
				if(!empty($idHd[1])){
					$select->where("hd.id = ?", $idHd[1]);
				}
				$select->where("(kh.name LIKE '%" . $idHd[0] . "%') OR (kh.code LIKE '%" . $idHd[0] . "%') OR (kh.email LIKE '%" . $idHd[0] . "%') OR (kh.phone LIKE '%" . $idHd[0] . "%') OR (kh.telephone LIKE '%" . $idHd[0] . "%') OR (kh.fax LIKE '%" . $idHd[0] . "%') OR (kh.address LIKE '%" . $idHd[0] . "%')");
			}
		
			if (!empty($ssFilter['blocks'])) {
				if($ssFilter['blocks'] == 'nohocphi') {
					$select 	->	where('hd.hd_sotienno > ?', 0);
				}
				if($ssFilter['blocks'] == 'chuavaolop') {
					$select 	->	where('hd.id_lophoc = ?', 0);
				}
			}
				
			$result = $db	-> fetchOne($select);
		}
	
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null) {
		$ssFilter  = $arrParam['ssFilter'];
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
	if ($options['task'] == 'admin-list') {
				
			$paginator = $arrParam['paginator'];
			$ssFilter  = $arrParam['ssFilter'];
			
			$select = $db	->	select()
							->	from('nam_hopdong AS hd', array('*', "DATE_FORMAT(ngayky, '%d/%m/%Y') AS ngayky"))
							-> 	joinLeft('nam_daotao_lophoc AS dtlh', 'dtlh.id = hd.id_lophoc', array('dtlh.name AS lophoc_name'))
							-> 	joinLeft('khachhang AS kh', 'kh.id = hd.id_kh', array('kh.name AS khachhang_name', 'kh.code AS khachhang_code'))			
							-> 	joinLeft('users AS u', 'u.id = hd.id_nhanvien', array('u.name AS nhanvien_name', 'u.alias AS nhanvien_alias'))
							->	where('hd.status != ?', -1);
			
			if (!empty($arrParam['id_kh'])) {
				$select 	->	where('hd.id_kh = ?', $arrParam['id_kh']);
			}
			
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
			
			if ($ssFilter['id_khoahoc'] > 0 ) {
				$select 	->	where('hd.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
			
			if ($ssFilter['id_lophoc'] > 0) {
				$select 	->	where('hd.id_lophoc = ?', $ssFilter['id_lophoc']);
			}
			
			if (!empty($ssFilter['keywords'])) {
				$idHd = explode('-', $ssFilter['keywords']);
				if(!empty($idHd[1])){
					$select->where("hd.id = ?", $idHd[1]);
				}
				$select->where("(kh.name LIKE '%" . $idHd[0] . "%') OR (kh.code LIKE '%" . $idHd[0] . "%') OR (kh.email LIKE '%" . $idHd[0] . "%') OR (kh.phone LIKE '%" . $idHd[0] . "%') OR (kh.telephone LIKE '%" . $idHd[0] . "%') OR (kh.fax LIKE '%" . $idHd[0] . "%') OR (kh.address LIKE '%" . $idHd[0] . "%')");
			}
			
			if (!empty($ssFilter['blocks'])) {
				if($ssFilter['blocks'] == 'nohocphi') {
					$select 	->	where('hd.hd_sotienno > ?', 0);
				}
				if($ssFilter['blocks'] == 'chuavaolop') {
					$select 	->	where('hd.id_lophoc = ?', 0);
				}
			}
			
			if ($paginator['itemCountPerPage'] > 0) {
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			//echo $select;die;
			$result = $db	-> fetchAll($select);
		}
		
		
		if ($options['task'] == 'admin-excel') {
		
			$paginator = $arrParam['paginator'];
			$ssFilter  = $arrParam['ssFilter'];
				
			$select = $db	->	select()
			->	from('nam_hopdong AS hd', array('*', "DATE_FORMAT(ngayky, '%d/%m/%Y') AS ngayky"))
			-> 	joinLeft('nam_daotao_lophoc AS dtlh', 'dtlh.id = hd.id_lophoc', array('dtlh.name AS lophoc_name'))
			-> 	joinLeft('khachhang AS kh', 'kh.id = hd.id_kh', array('kh.name AS khachhang_name', 'kh.code AS khachhang_code'))
			-> 	joinLeft('users AS u', 'u.id = hd.id_nhanvien', array('u.name AS nhanvien_name', 'u.alias AS nhanvien_alias'))
			->	where('hd.status != ?', -1);
				
			if (!empty($arrParam['id_kh'])) {
				$select 	->	where('hd.id_kh = ?', $arrParam['id_kh']);
			}
				
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
				
			if ($ssFilter['id_khoahoc'] > 0 ) {
				$select 	->	where('hd.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
				
			if ($ssFilter['id_lophoc'] > 0) {
				$select 	->	where('hd.id_lophoc = ?', $ssFilter['id_lophoc']);
			}
				
			if (!empty($ssFilter['keywords'])) {
				$idHd = explode('-', $ssFilter['keywords']);
				if(!empty($idHd[1])){
					$select->where("hd.id = ?", $idHd[1]);
				}
				$select->where("(kh.name LIKE '%" . $idHd[0] . "%') OR (kh.code LIKE '%" . $idHd[0] . "%') OR (kh.email LIKE '%" . $idHd[0] . "%') OR (kh.phone LIKE '%" . $idHd[0] . "%') OR (kh.telephone LIKE '%" . $idHd[0] . "%') OR (kh.fax LIKE '%" . $idHd[0] . "%') OR (kh.address LIKE '%" . $idHd[0] . "%')");
			}
				
			if (!empty($ssFilter['blocks'])) {
				if($ssFilter['blocks'] == 'nohocphi') {
					$select 	->	where('hd.hd_sotienno > ?', 0);
				}
				if($ssFilter['blocks'] == 'chuavaolop') {
					$select 	->	where('hd.id_lophoc = ?', 0);
				}
			}
				
			if ($paginator['itemCountPerPage'] > 0) {
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			//echo $select;die;
			$result = $db	-> fetchAll($select);
		}
		
		if ($options['task'] == 'admin-recyclebin') {
		
			$paginator = $arrParam['paginator'];
			$ssFilter  = $arrParam['ssFilter'];
				
			$select = $db	->	select()
			->	from('nam_hopdong AS hd', array('*', "DATE_FORMAT(ngayky, '%d/%m/%Y') AS ngayky"))
			-> 	joinLeft('nam_daotao_lophoc AS dtlh', 'dtlh.id = hd.id_lophoc', array('dtlh.name AS lophoc_name'))
			-> 	joinLeft('khachhang AS kh', 'kh.id = hd.id_kh', array('kh.name AS khachhang_name', 'kh.code AS khachhang_code'))
			-> 	joinLeft('users AS u', 'u.id = hd.id_nhanvien', array('u.name AS nhanvien_name', 'u.alias AS nhanvien_alias'))
			->	where('hd.status = -1');
				
			if (!empty($arrParam['id_kh'])) {
				$select 	->	where('hd.id_kh = ?', $arrParam['id_kh']);
			}
				
			if (!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
				
			if ($ssFilter['id_khoahoc'] > 0 ) {
				$select 	->	where('hd.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
				
			if ($ssFilter['id_lophoc'] > 0) {
				$select 	->	where('hd.id_lophoc = ?', $ssFilter['id_lophoc']);
			}
				
			if (!empty($ssFilter['keywords'])) {
				$idHd = explode('-', $ssFilter['keywords']);
				if(!empty($idHd[1])){
					$select->where("hd.id = ?", $idHd[1]);
				}
				$select->where("(kh.name LIKE '%" . $idHd[0] . "%') OR (kh.code LIKE '%" . $idHd[0] . "%') OR (kh.email LIKE '%" . $idHd[0] . "%') OR (kh.phone LIKE '%" . $idHd[0] . "%') OR (kh.telephone LIKE '%" . $idHd[0] . "%') OR (kh.fax LIKE '%" . $idHd[0] . "%') OR (kh.address LIKE '%" . $idHd[0] . "%')");
			}
				
			if (!empty($ssFilter['blocks'])) {
				if($ssFilter['blocks'] == 'nohocphi') {
					$select 	->	where('hd.hd_sotienno > ?', 0);
				}
				if($ssFilter['blocks'] == 'chuavaolop') {
					$select 	->	where('hd.id_lophoc = ?', 0);
				}
			}
				
			if ($paginator['itemCountPerPage'] > 0) {
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
				
			$result = $db	-> fetchAll($select);
		}
		
		return $result;
	}
	
	public function deleteItem($arrParam = null, $options = null) {
		if ($options['task'] == 'admin-delete') {
			$where 	= ' id=' . $arrParam['id'];
			$data	= array(
						'status' => -1
					);
			$result = $this->update($data, $where);
		}
	
		if ($options['task'] == 'admin-delete-muti') {
			$cid = explode(',', $arrParam['cid']);
			if (!empty($cid) && isset($arrParam['cid'])) {
				$ids = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$data	= array(
						'status' => -1
				);
				$this->update($data, $where);
			}
		}
	}
	
	public function changeStatus($arrParam = null, $options = null) {
		$cid = $arrParam['cid'];
		if($arrParam['id'] > 0){
			if($arrParam['type'] == 1){
				$status = 1;
				// 			}else{
				// 				$status = 0;
			}
			$data = array(	'status' =>$status,
					//'created' => @date("Y-m-d H:i:s"),
					//'modified'=> @date("Y-m-d H:i:s"),
			);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		
		}
		

	}
	

	public function saveItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		
		if($options['task'] == 'admin-add'){
			$thanhtoan	= array(
						'thanhtoan_ngaydong' 	=> $arrParam['thanhtoan_ngaydong'],
						'thanhtoan_sotien' 		=> $arrParam['thanhtoan_sotien'],
						'thanhtoan_ghichu' 		=> $arrParam['thanhtoan_ghichu'],
						'thanhtoan_ngaysua' 	=> $arrParam['thanhtoan_ngaysua'],
						'thanhtoan_nguoitao' 	=> $arrParam['thanhtoan_nguoitao'],
						'thanhtoan_id_nguoitao' => $arrParam['thanhtoan_id_nguoitao']
					);
			$thanhtoan = serialize($thanhtoan);
			
			if(!empty($arrParam['id_khuyenmai'])){
				$id_khuyenmai = implode(',', $arrParam['id_khuyenmai']);
			}
			
			// Them vao bang ca nhan
			$date     = new Zend_Date();
			if (!empty($arrParam['ngayky'])) {
				$date->set($arrParam['ngayky']);
			} else {
				$date->set('00/00/0000');
			}
			$ngayky    = $date->toString('YYYY-MM-dd');
			
			$row 				= $this->fetchNew();

			$info 					= new Zendvn_System_Info();
			$created_by 			= $info->getMemberInfo('id');
			
			$row->id_kh 			= $arrParam['khachhang']['id'];
			$row->id_nhanvien		= $arrParam['khachhang']['id_nhanvien'];
			$row->id_khoahoc 		= $arrParam['id_khoahoc'];
			$row->id_lophoc 		= $arrParam['id_lophoc'];
			$row->id_khuyenmai 		= $id_khuyenmai;
			
			$row->hd_dongia 		= $arrParam['dongia'];
			$row->hd_thanhtien 		= $arrParam['hd_thanhtien'];
			$row->hd_sotiendadong	= $arrParam['hd_sotiendadong'];
			$row->hd_sotienno		= $arrParam['hd_sotienno'];
			
			$row->status 			= 1;
			$row->content 			= '';
			$row->thanhtoan			= $thanhtoan;
			$row->ngayky 			= $ngayky;
			$row->created 			= @date("Y-m-d H:i:s");
			$row->created_by		= $created_by;

			$lastId = $row->save();

			// Up ma kh
			$table = 'khachhang';
			$bind = array(
						'code' => 'nv' . $arrParam['khachhang']['id_nhanvien'] . 'kh' . $arrParam['khachhang']['id'],
						'status_khachhang' => 1
					);
			$where = ' id = ' . $arrParam['khachhang']['id'];
			$db->update($table, $bind, $where);
			
			// Cap nhat log he thong
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Thêm hợp đồng mới - id ' . $lastId,
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
	
		if($options['task'] == 'admin-edit'){
			// Them vao bang ca nhan
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);

			$info 					= new Zendvn_System_Info();
			$modified_by 			= $info->getMemberInfo('id');
			$row->diemdauvao 		= $arrParam['diemdauvao'];
			$row->diemdaura 		= $arrParam['diemdaura'];
			$row->diemgiuaky	 	= $arrParam['diemgiuaky'];
			$row->diemgiaovien 		= $arrParam['diemgiaovien'];
			$row->diemcoach 		= $arrParam['diemcoach'];
			$row->diemlophoc 		= $arrParam['diemlophoc'];
			$row->sobuoidiemdanh 	= $arrParam['sobuoidiemdanh'];
			

			$row->modified 			= @date("Y-m-d H:i:s");
			$row->modified_by		= $created_by;
			
			$row->save();
			$lastId = $arrParam['id'];
			// Cap nhat log he thong
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Sửa hợp đồng - id ' . $lastId,
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
		
		return $lastId;
	}
	
}